import { httpPost, httpPut, httpGet } from '../http'

// 微信登录接口
export function wxLogin(data) {
  return httpPost('/api/wxlogin/getPhoneNumber', {
    code: data.code
  });
}

// 微信注册接口
export function wxRegister(userInfo) {
  return httpPost('/api/wx/register', {
    userName: userInfo.userName,
    password: userInfo.password
  })
}

//更新用户信息（旧废弃）
export function updateUserInfo(userInfo) {
  return httpPut('/api/wx/update-info', {
    userId: userInfo.userId,
    name: userInfo.name,
    phone: userInfo.phone,
    address: userInfo.address,
    selfCareStatus: userInfo.selfCareStatus
  });
}

//用户购买服务
export function userBuyService(purchaseData) {
  return httpPost('/api/wx/purchase-service', {
    userId: purchaseData.userId,
    serviceName: purchaseData.serviceName,
    appointmentDate: purchaseData.appointmentDate
  });
}

// 获取用户信息
export function getWxUserInfo(userId) {
  return httpGet(`/api/wx/get-info/${userId}`);
} 

//用户预约记录
export function getUserAppointmentRecord(userId) {
  return httpGet(`/api/wx/service-appointments/${userId}`);
}

//更新用户点击服务记录
export function clickservice(userId, serviceName) {
  return httpPut('/api/wxlogin/click-service', {
    userId: Number(userId),
    serviceName: String(serviceName)
  });
}

//更新wx-user表中用户信息
export function updateWxUserInfo(userId, userInfo) {
  return httpPut('/api/wxlogin/update', {
    id: Number(userId),
    phoneNumber: userInfo.phoneNumber,
    headImage: userInfo.headImg,
    birthday: userInfo.birthday,
    // gender: userInfo.gender === '男' ? 0 : 1,
  });
}

//后端获取openid
export function getOpenId(code) {
  return httpPost('/api/wxlogin/code2Session', {
    code: code
  });
}

//获取prepay_id
export function getPrepayId(data) {
  // 保证 totalPrice 有值且为数字，元转分并取整
  const totalFee = Math.round(Number(data.totalPrice) * 100);
  return httpPost('/api/wx/pay/create-order', {
    description: data.serviceName || "测试商品1",
    outTradeNo: data.outTradeNo,
    totalFee: totalFee > 0 ? totalFee : 1, // 微信支付最小金额为1分
    openid: data.openid
  });
}

//创建商品订单
export function createProduceOrder(data) {
  return httpPost('/api/shop-orders', {
    wxUserId: data.wxUserId,
    phone: data.phone, 
    name: data.name,
    address: data.address,
    amount: data.amount,
    outTradeNo: data.outTradeNo,
    productList: data.productList, 
  });
}

//获取医院地址列表
export function getHospitalAddressList(data) {
  return httpGet('/api/hospital-address'); 
}

//获取用户商品订单列表
export function getUserOrderList(data) {
  return httpGet(`/api/shop-orders/by-wx-user/${data.wxUserId}`);
}

//获取全部用户商品订单列表
export function getAllUserOrderList() {
  return httpGet('/api/shop-orders');
}

//更新用户商品订单状态
export function updateUserOrderStatus(data) {
  return httpPut(`/api/shop-orders/${data.id}`, {
    
    status: data.status
  });
}
